﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.IO;
using hcom;
using System.Data;
using System.Text;

namespace hcms.Admin
{
	public partial class homeTemplate : System.Web.UI.Page
	{
		public string submitstate;
		//private string id;
		private string path, pathhtm;
		private hString hstr = new hString();
		public Model model = Global.model;
		protected void Page_Load(object sender, EventArgs e)
		{
			path = Request.PhysicalApplicationPath + "homeTemplate.aspx";		//取网站虚拟目录的实际文件夹
			pathhtm = Request.PhysicalApplicationPath + "index.htm";

			if (Page.IsPostBack)
			{
				switch (cmd.Value)
				{
					case "save":
						save();
						return;
					default:
						throw( new Exception( "不可识别的命令: \"" + cmd.Value + "\"" ));
				}
			}
			else
				code.Value = File.ReadAllText(path);
		}

		private void save()
		{
			File.WriteAllText(path, code.Value, System.Text.Encoding.GetEncoding("utf-8"));

			//保存静态文件 index.htm
			using (FileStream fs = new FileStream(pathhtm, FileMode.Create, FileAccess.Write, FileShare.Write))
			{
				using (StreamWriter streamwriter = new StreamWriter(fs, System.Text.Encoding.GetEncoding("utf-8")))
				{
					System.Threading.Thread.Sleep(10);	//?? 在本地测试总是读上次的缓存，用IIS服务器可能就好了。改用IIS后有时还不行，加上10毫米的延时就好了
					Server.Execute("/homeTemplate.aspx", streamwriter);
				}
			}

			submitstate = "保存完毕";
			//ClientScript.RegisterClientScriptBlock(this.GetType(), "script", "alert('保存完毕。')", true);
		}
	
	}
}